/* rcctwba1.p - Container Workbench Record Sort Routine                       */
/* Copyright 1986-2002 QAD Inc., Carpinteria, CA, USA.                        */
/* All rights reserved worldwide.  This is an unpublished work.               */
/* $Revision: 1.3.1.2 $                                                         */
/*V8:ConvertMode=NoConvert                                                    */
/*                  LAST MODIFIED: 04/14/95           BY: GWM *J049*          */
/*                  LAST MODIFIED: 06/05/96           BY: GWM *J0QY*          */
/* REVISION: 8.6    LAST MODIFIED: 05/20/98   BY: *K1Q4* Alfred Tan           */
/* REVISION: 9.1    LAST MODIFIED: 08/12/00   BY: *N0KP* Mark Brown           */
/* $Revision: 1.3.1.2 $    BY: Jean Miller    DATE: 03/22/01 ECO: *P008*          */
/******************************************************************************/
/* All patch markers and commented out code have been removed from the source */
/* code below. For all future modifications to this file, any code which is   */
/* no longer required should be deleted and no in-line patch markers should   */
/* be added.  The ECO marker should only be included in the Revision History. */
/******************************************************************************/

{mfdeclre.i}

/* INPUT PARAMETERS */
define input parameter abs_recid as recid.
define input parameter cont_level as integer.
define input parameter tmp_prefix as character.

/* INPUT-OUTPUT VARIABLES */
define input-output parameter next_nbr as integer.

/* LOCAL VARIABLES */
define variable ship_from like abs_shipfrom no-undo.
define variable par_id like abs_par_id no-undo.

main:
do:

   /* CHECK MAX LEVELS */
   if cont_level > 9 then do:
      next_nbr = -1.
      leave main.
   end.

   find abs_mstr where recid(abs_mstr) = abs_recid
   exclusive-lock no-wait no-error.

   if locked(abs_mstr) then do:
      next_nbr = -2.
      leave main.
   end.

   /* IF A NON TEMP NUMBER THEN SAVE */
   if index(abs_mstr.abs_shipto,"::") = 0 then
      abs_mstr.abs__qad05 = abs_mstr.abs_shipto.

   /* THESE FIELDS MAY BE LONGER THAN SCHEMA DEF */
   assign
      abs_mstr.abs_shipto = tmp_prefix + string(next_nbr,"99999")
      par_id = abs_mstr.abs_id.

   assign
      abs_mstr.abs__qad06 = string(cont_level,"9")
      ship_from = abs_mstr.abs_shipfrom
      cont_level = cont_level + 1.

   /* SORT ITEMS FIRST */
   for each abs_mstr where
            abs_mstr.abs_shipfrom = ship_from
        and abs_mstr.abs_par_id = par_id
        and abs_mstr.abs_id begins "I"
   no-lock:

      next_nbr = next_nbr + 1.

      if next_nbr > 9999 then leave main.

      /* SET ORDER AND LEVEL FOR CHILD RECORDS */
      {gprun.i ""rcctwba1.p""
         "(input recid(abs_mstr),
           input cont_level,
           input tmp_prefix,
           input-output next_nbr)"}

      if next_nbr < 0 or next_nbr > 9999 then leave main.

   end.

   /* SORT FOR CONTAINERS */
   for each abs_mstr where
            abs_mstr.abs_shipfrom = ship_from
        and abs_mstr.abs_par_id = par_id
        and abs_mstr.abs_id begins "C"
   no-lock:

      next_nbr = next_nbr + 1.

      if next_nbr > 9999 then leave main.

      /* SET ORDER AND LEVEL FOR CHILD RECORDS */
      {gprun.i ""rcctwba1.p""
         "(input recid(abs_mstr),
           input cont_level,
           input tmp_prefix,
           input-output next_nbr)"}

      if next_nbr < 0 or next_nbr > 9999 then leave main.

   end.

end. /* main */
